/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.client.web.tools;

import org.peony.standard.tools.SIConvertText;
import org.peony.tools.StringUtil;

/**
 *  
 *
 *  @author     陈慧然
 *  @version    Id: HtmlTextConvert.java, v 0.000 2008-4-17 下午09:46:57 陈慧然 Exp
 */
public class HtmlTextConvert implements SIConvertText{
	
	private static HtmlTextConvert instance = new HtmlTextConvert();
	
	private HtmlTextConvert(){}
	
	private static final String AND = "&";
	private static final String CAND = "&amp;";
	private static final String LESS = "<";
	private static final String CLESS = "&lt;";
	private static final String GREATER = ">";
	private static final String CGREATER = "&gt;";
	private static final String QUOTE = "\"";
	private static final String CQUOTE = "&quot;";
	private static final String SIGLEQUOTE = "'";
	private static final String CSIGLEQUOTE = "\'";
	private static final String NEWLINE = "\n";
	private static final String STARTAPRA = "<p>";
	private static final String ENDAPRA = "</p>";
	private static final String APRA = ENDAPRA + STARTAPRA;
	private static final String SPACE = " ";
	private static final String CSPACE = "&nbsp;";
	private static final String EMPTY = StringUtil.empty;
	
	public String dataToShow(String text) {
		if(StringUtil.isEmpty(text))
			return StringUtil.empty;
		if(text.indexOf(AND) >= 0)
			text = StringUtil.replace(text, AND, CAND);
		if(text.indexOf(LESS) >= 0)
			text = StringUtil.replace(text, LESS, CLESS);
		if(text.indexOf(GREATER) >= 0)
			text = StringUtil.replace(text, GREATER, CGREATER);
		if(text.indexOf(QUOTE) >= 0)
			text = StringUtil.replace(text, QUOTE, CQUOTE);
		if(text.indexOf(SIGLEQUOTE) >= 0)
			text = StringUtil.replace(text, SIGLEQUOTE, CSIGLEQUOTE);
		if(text.indexOf(NEWLINE) >= 0)
			text = STARTAPRA + StringUtil.replace(text, NEWLINE, APRA) + ENDAPRA;
		if(text.indexOf(SPACE) >= 0)
			text = StringUtil.replace(text, SPACE, CSPACE);
		return text;
	}

	public static SIConvertText getInstance() {
		return instance;
	}

	public String showToData(String text) {
		if(StringUtil.isEmpty(text))
			return StringUtil.empty;
		if(text.indexOf("<br") >= 0)
			text = StringUtil.replace(text, "<br>", "\n");
		if(text.indexOf("<br/>") >= 0)
			text = StringUtil.replace(text, "<br/>", "\n");
		if(text.indexOf("<br />") >= 0)
			text = StringUtil.replace(text, "<br />", "\n");
		if(text.indexOf(ENDAPRA) >= 0){
			text = StringUtil.replace(text, ENDAPRA, NEWLINE);
			text = StringUtil.replace(text, STARTAPRA, EMPTY);
		}
		if(text.indexOf(CLESS) >= 0)
			text = StringUtil.replace(text, CLESS, LESS);
		if(text.indexOf(CGREATER) >= 0)
			text = StringUtil.replace(text, CGREATER, GREATER);
		if(text.indexOf(CQUOTE) >= 0)
			text = StringUtil.replace(text, CQUOTE, QUOTE);
		if(text.indexOf(CSPACE) >= 0)
			text = StringUtil.replace(text, CSPACE, SPACE);
		if(text.indexOf(CAND) >= 0)
			text = StringUtil.replace(text, CAND, AND);
		return text;
	}
	
}
